﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IUAP.DAL;
using IUAP.DAL.Articulo;

namespace IUAP.BLL.Articulo
{
    public class Comentario : BaseArticulo
    {
        private string mail_usua_come = "";

        public string Mail_usua_come
        {
            get { return mail_usua_come; }
            private set { mail_usua_come = value; }
        }

        private string ip_usua_come = "";

        public string Ip_usua_come
        {
            get { return ip_usua_come; }
            private set { ip_usua_come = value; }
        }

        private int id_articulo = 0;

        public int Id_articulo
        {
            get { return id_articulo; }
            private set { id_articulo = value; }
        }

        private string titu_arti = "";

        public string Titu_arti
        {
            get 
            {
                titu_arti = Article.Titu_arti;
                return titu_arti; 
            }
            set { titu_arti = value; }
        }

        private Articulo articulo = null;
        public Articulo Article
        {
            get
            {
                if (articulo == null)
                    articulo = Articulo.GetArticleByID(this.Id_articulo);
                return articulo;
            }
        }

        private string cont_come = "";

        public string Cont_come
        {
            get { return cont_come; }
            set { cont_come = value; }
        }

        public string EncodedBody
        {
            get { return Helpers.ConvertToHtml(this.Cont_come); }
        }

        public Comentario(int id_comentario, DateTime fecha_come, string usua_come, string mail_usua_come,
         string ip_usua_come, int id_articulo, string titu_arti,string cont_come)
        {
            this.Id = id_comentario;
            this.Fecha = fecha_come;
            this.Usuario = usua_come;
            this.Mail_usua_come = mail_usua_come;
            this.Ip_usua_come = ip_usua_come;
            this.Id_articulo = id_articulo;
            this.Titu_arti = titu_arti;
            this.Cont_come = cont_come;
        }

        public bool Delete()
        {
            bool success = Comentario.DeleteComment(this.Id);
            if (success)
                this.Id = 0;
            return success;
        }

        public bool Update()
        {
            return Comentario.UpdateComment(this.Id, this.Cont_come);
        }

        //************* METODOS ESTATICOS *******************

        public static List<Comentario> GetComments()
        {
            List<Comentario> comments = GetComments(0, BizObject.MAXROWS);
            comments.Sort(new CommentComparer("Fecha_come ASC"));
            return comments;
        }
        public static List<Comentario> GetComments(int startRowIndex, int maximumRows)
        {
            List<Comentario> comments = null;
            string key = "Articles_Comments_" + startRowIndex.ToString() + "_" + maximumRows.ToString();

            if (BaseArticulo.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                comments = (List<Comentario>)BizObject.Cache[key];
            }
            else
            {
                List<ComentarioDetalles> recordset = SiteProvider.Articulos.GetComentario(
                GetPageIndex(startRowIndex, maximumRows), maximumRows);
                comments = GetCommentListFromCommentDetailsList(recordset);
                BaseArticulo.CacheData(key, comments);
            }
            return comments;
        }
        public static List<Comentario> GetComments(int id_articulo)
        {
            List<Comentario> comments = GetComments(id_articulo, 0, BizObject.MAXROWS);
            comments.Sort(new CommentComparer("Fecha_come ASC"));
            return comments;
        }
        public static List<Comentario> GetComments(int id_articulo, int startRowIndex, int maximumRows)
        {
            List<Comentario> comments = null;
            string key = "Articles_Comments_" + id_articulo.ToString() + "_" + startRowIndex.ToString() + "_" + maximumRows.ToString();

            if (BaseArticulo.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                comments = (List<Comentario>)BizObject.Cache[key];
            }
            else
            {
                List<ComentarioDetalles> recordset = SiteProvider.Articulos.GetComentariobyArticulo(id_articulo,
                GetPageIndex(startRowIndex, maximumRows), maximumRows);
                comments = GetCommentListFromCommentDetailsList(recordset);
                BaseArticulo.CacheData(key, comments);
            }
            return comments;
        }
        public static int GetCommentCount()
        {
            int commentCount = 0;
            string key = "Articles_CommentCount";

            if (BaseArticulo.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                commentCount = (int)BizObject.Cache[key];
            }
            else
            {
                commentCount = SiteProvider.Articulos.GetContarComentario();
                BaseArticulo.CacheData(key, commentCount);
            }
            return commentCount;
        }
        public static int GetCommentCount(int id_articulo)
        {
            int commentCount = 0;
            string key = "Articles_CommentCount_" + id_articulo.ToString();

            if (BaseArticulo.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                commentCount = (int)BizObject.Cache[key];
            }
            else
            {
                commentCount = SiteProvider.Articulos.GetContarComentariobyArticulo(id_articulo);
                BaseArticulo.CacheData(key, commentCount);
            }
            return commentCount;
        }
        public static Comentario GetCommentByID(int id_comentario)
        {
            Comentario comment = null;
            string key = "Articles_Comment_" + id_comentario.ToString();

            if (BaseArticulo.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                comment = (Comentario)BizObject.Cache[key];
            }
            else
            {
                comment = GetCommentFromCommentDetails(SiteProvider.Articulos.GetComentariobyID(id_comentario));
                BaseArticulo.CacheData(key, comment);
            }
            return comment;
        }
        public static bool UpdateComment(int Id, string Cont_come)
        {
            ComentarioDetalles record = new ComentarioDetalles(
               Id, DateTime.Now, "", "", "", 0,Cont_come);
            bool ret = SiteProvider.Articulos.UpdateComentario(record);
            BizObject.PurgeCacheItems("articles_comment");
            return ret;
        }
        public static bool DeleteComment(int ID)
        {
            bool ret = SiteProvider.Articulos.DeleteComentario(ID);
            new RecordDeletedEvent("comment", ID, null).Raise();
            BizObject.PurgeCacheItems("articles_comment");
            return ret;
        }
        public static int InsertComment(string usuario, string Mail_usua_come, int id_articulo, string  Cont_come)
        {
            ComentarioDetalles record = new ComentarioDetalles(0, DateTime.Now, usuario,
               Mail_usua_come, BizObject.CurrentUserIP, id_articulo, Cont_come);
            int ret = SiteProvider.Articulos.InsertComentario(record);
            BizObject.PurgeCacheItems("articles_comment");
            return ret;
        }
        private static Comentario GetCommentFromCommentDetails(ComentarioDetalles record)
        {
            if (record == null)
                return null;
            else
            {
                return new Comentario(record.Id_comentario, record.Fech_come, record.Usua_come,
                   record.Mail_usua_come, record.Ip_usua_come,record.Id_articulo,record.Titu_arti,record.Cont_come);
            }
        }
        private static List<Comentario> GetCommentListFromCommentDetailsList(List<ComentarioDetalles> recordset)
        {
            List<Comentario> comments = new List<Comentario>();
            foreach (ComentarioDetalles record in recordset)
                comments.Add(GetCommentFromCommentDetails(record));
            return comments;
        }

        public class CommentComparer : IComparer<Comentario>
        {
            private string _sortBy;
            private bool _reverse;

            public CommentComparer(string sortBy)
            {
                if (!string.IsNullOrEmpty(sortBy))
                {
                    sortBy = sortBy.ToLower();
                    _reverse = sortBy.EndsWith(" desc");
                    _sortBy = sortBy.Replace(" desc", "").Replace(" asc", "");
                }
            }

            public bool Equals(Comentario x, Comentario y)
            {
                return (x.Id == y.Id);
            }

            public int Compare(Comentario x, Comentario y)
            {
                int ret = 0;
                switch (_sortBy)
                {
                    case "fecha_come":
                        ret = DateTime.Compare(x.Fecha, y.Fecha);
                        break;
                    case "usua_come":
                        ret = string.Compare(x.Usuario, y.Usuario, StringComparison.InvariantCultureIgnoreCase);
                        break;
                }
                return (ret * (_reverse ? -1 : 1));
            }
        }
    }

}